<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>搜索</title>
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_2384888_ojge9md3tr.css
    ">
    <link rel="shortcut icon" href="../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" href="../css/online.css">
</head>

<body>
    <!-- 导航菜单 -->
    <nav>
        <div class="main">
            <img src="../img/public/logo.png" alt="" class="logo">
            <ul>
                <li><a href="../index.html">首页</a></li>
                <li class="check"><a href="javascript:;">同步课程</a></li>
                <li><a href="javascript:;">精品课程</a></li>
            </ul>
            <div class="search">
                <div class="left">课程<i class="iconfont icon-down"></i>
                </div>
                <input type="text" placeholder="搜索感兴趣的内容" id="keyword">
                <img src="../img/public/search.png" alt="" id="searchBtn">
            </div>
            <div class="login">
                <a href="javascript:;">登陆</a>
                /
                <a href="javascript:;">注册</a>
            </div>
            <div class="user">
                <img src="../img/login/user.png" alt="">
                <div class="userlist">
                    <ul>
                        <li id="ImUsername">我是用户名</li>
                        <li><a href="./course.html">课程中心</a></li>
                        <li>订单中心</li>
                        <li>资金管理</li>
                        <li><a href="./userSeting.html">个人中心</a></li>
                        <li class="out">退出登陆</li>
                    </ul>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主体内容 -->
    <main>
        <div class="search_keyword" id="show_searchNum"></div>
        <div class="lesson">
            <ul id="search_course">

                <!-- <li>
                    <div class="top">
                        <img src="../img/online/banner1-1.png" alt="" class="m">
                        <p>学科：英语</p>
                    </div>
                    <div class="bottom">
                        <div class="left">
                            <span>人教版语文第一节课程内容讲解</span>
                            <span class="time">地区：北京</span>
                        </div>
                        <div class="right">免费学习</div>
                    </div>
                </li> -->
            </ul>
        </div>

        <div class="pageNum">
            <ul id="page">
                <!-- <li class="active">1</li>
                <li>2</li>
                <li>3</li> -->
            </ul>
        </div>
    </main>
    <style>
        .search_keyword {
            font-size: 16px;
        }

        .search_keyword span {
            color: red;
        }
    </style>
    <footer>
        <div class="cont">
            <div class="main">
                <p>
                    首页 | 关于我们 | 加入我们 | 合作专区 | 联系我们 | AI开放平台 | 意见反馈 | 漏洞提交 | 隐私政策 | 版权声明 | 反盗链声明 | 网上有害信息举报 | 备案公示 | 营业执照
                    | 教师资格证公示

                </p>
                <p>
                    京ICP备 13030888号 Copyright © 2014-2019 行者信息科技（北京）有限公司 | 地址：北京市新华区马当路388号C座 | 电话：010-66666666 | 京公网安备
                    01010102002533号
                </p>
                <p>
                    京网文〔2018〕4086-308号 | 网络文化经营许可证：沪网文[2018]4086-308号 | 增值电信业务经营许可证：京B2-20150021 |
                    食品经营许可证：JY13101140088888
                </p>
                <p>医疗器械经营许可证：京嘉食药监械经营许20188008号 | 互联网药品信息服务资格证书：(京)-经营性-2018-0011 |</p>
            </div>
        </div>
    </footer>



    <!-- <script src="../js/ujiuye.js"></script> -->
</body>
<script src="../utils/jquery.min.js"></script>
<script src="../utils/Http.js" type="module"></script>
<script src="../js/myRender.js" type="module"></script>
<script type="module">
    import Http from '../utils/Http.js'
    import {userinfo,showLoginAndRegister,fsToObj} from '../js/myRender.js'
    showLoginAndRegister()
    // getSearch()
    //获取搜索到的数据 
    let str = ' ';
    let nowPage = 1;
    async function getSearch() {
        let obj = fsToObj(location.search); //获取查询字符串
        let sendKeyword = obj.keyword || str //获取发送的关键词
        let {
            result
        } = await Http({
            url: '/api/search',
            data: {
                page: nowPage,
                keyword: sendKeyword,
            }
        })
        // console.log(result);
        if (result.status != 200) {
            renderSearchData(sendKeyword, 0, 0)
            renderSearchCourse([])
            renderSearchPage(0, 0)
            return;
        }
        let {
            data,
            keyword,
            page,
            pageSize,
            rows,
            totalPage
        } = result.result

        //渲染搜索数据
        renderSearchData(keyword, rows, totalPage)
        //渲染搜索出来的课程
        renderSearchCourse(data)
        //渲染页码
        renderSearchPage(page, totalPage)
    }
    //点击按钮获取val的值
    $('#searchBtn').on('click', function () {
        if ($('#keyword').val() == '') {
            alert('输入不能为空')
            return;
        }
        nowPage = 1;
        str = $('#keyword').val() //把搜索的内容给str 给到ajax参数
        $('#keyword').val('')
        getSearch()
    })
    getSearch()
    //渲染搜索数据
    function renderSearchData(keyword, rows, totalPage) {
        let searchStr = `搜索到 <span id="nowKeyword">${keyword}</span> 关键字共<span>${rows}</span>
            条，共<span>${totalPage}</span>页`;
        if (rows == 0) {
            searchStr +=
                '<div class="goToIndex" style="width: 200px;margin: auto; font-size: 30px">跳转到<a href="../index.html">首页</a> </div>'
        }
        $('#show_searchNum').html(searchStr)
    }
    //渲染搜索出来的课程
    function renderSearchCourse(data) {
        let search_course = ''
        $('#search_course').html(search_course);
        data.forEach(data => {
            search_course += `
                <li>
                    <div class="top">
                        <img src="${data.image_src}" alt="" class="m">
                        <p>学科：${data.subject_name}</p>
                    </div>
                    <div class="bottom">
                        <div class="left">
                            <span>${data.title}</span>
                            <span class="time">地区：${data.area_name}</span>
                        </div>
                        <div class="right">${data.price == 0 ? '免费学习' : data.price +'元'}</div>
                    </div>
                </li>
                `;
        })
        $('#search_course').html(search_course);
    }
    //渲染页码
    function renderSearchPage(page, totalPage) {
        let pageLi = '';
        for (var i = 0; i < totalPage; i++) {
            pageLi += `<li t="nowPage" v="${i+1}" class="${page == i+1 ?'active':''}">${i+1}</li>`;
        }
        $('#page').html(pageLi);
    }
    $('#page').on('click', 'li', function () {
        nowPage = $(this).html() - 0;
        getSearch()
    })
</script>

</html>